<?php
if (!defined('W_ROOT')) {
    exit();
}
$_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];
$pharmacy_id = $_SESSION['acc_id'];
$qstring = '';
$rowsPerPage = isset($_GET['num']) ? $_GET['num'] : '10';
if ((isset($_GET['catId']) && (int) $_GET['catId'] > 0)) {
    $changeCate = $_GET['catId'];
    $sql2 = "AND pic.category_id = $changeCate";
    $qstring = "catId=$changeCate";
} else {
    $changeCate = 0;
    $sql2 = '';
    $qstring = '';
}
$categoryList = buildCategoryOptions($changeCate);
$sql = "SELECT p.*,pip.*,pic.*, COUNT(pip.country_id) AS num_count FROM tblproduct AS p,tblproduct_in_pharmacy AS pip, tblproduct_in_category AS pic WHERE p.id = pip.product_id AND pip.pharmacy_id = $pharmacy_id AND pic.product_id = pip.product_id $sql2 GROUP BY pip.product_id ORDER BY pip.pid DESC";
$result = mysql_query(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage);
$row_num = mysql_num_rows($result);
?>
<style>
    .actionSingle a{
        color: #999; font-size: 90%;
    }
    .actionSingle a:hover{
        color: #000099; font-size: 90%;
    }
    .actionSingle a:hover.delete{
        color: #990000; font-size: 90%;
    }
</style>
<div class="row">
    <div class="box">
        <form action="#" method="post" name="frmlist">
            <div>
                <div class="block-header">
                    <h4><!--Product list--><?php echo $lang['PRO_LIST']; ?></h4>
                </div>
                <div style="float:left; padding-top:15px;">
                    <select name="cboCategory" id="cboCategory" onChange="viewProduct();">
                        <option value="0"><!--View By Category--><?php echo $lang['VIEW_BY_CAT']; ?></option>
                        <?php echo $categoryList; ?>
                    </select>
                </div>
                <div style="float:right; padding-top:15px;">
                    <input class="btn" type="button" name="btnadd" value="<?php echo $lang['ADD_NEW']; ?>" onClick="javascript:window.location.href='index.php?view=add&amp'; return false;" />&nbsp;&nbsp;
                    <input class="btn" type="button" name="btnpublish" value="<?php echo $lang['PUBLISH']; ?>" onclick="publishSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>','No item is selected for Publishing!',1,'Publish(s)','process')" />&nbsp;&nbsp;
                    <input class="btn" type="button" name="btnunpublish" value="<?php echo $lang['UNPUBLISH']; ?>" onclick="publishSelected(document.frmlist, '<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>','No item is selected for Unpublishing!',0,'Publish(s)','process')" />
                    <input class="btn" type="button" name="btndelete" value="<?php echo $lang['MY_DEL']; ?>" onclick="deleteSelected(document.frmlist,'<?php echo htmlspecialchars(getAllGetParams(array('action', 'view'))); ?>','No item is selected for Delete!','process','delete(s)')" />
                </div>
            </div>
            <div style="clear:both;">&nbsp;</div>
            <div class="box">

                <div class="center_admin">
                    <table cellpadding="0" cellspacing="0" border="0" width="100%" class="table table-striped table-hover">
                        <tbody>
                            <?php
                            $i = 1;
                            if (dbNumRows($result) > 0) {
                                while ($row = mysql_fetch_assoc($result)) {
                                    extract($row);

                                    if ($i % 2) {
                                        $class = 'row1';
                                    } else {
                                        $class = 'row2';
                                    }

                                    $sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = $id";
                                    $result_img = mysql_query($sql_img);
                                    $row_img = mysql_fetch_assoc($result_img);
                                    $row_img['thumbnail'];

                                    $country = mysql_query("SELECT COUNT(DISTINCT country_id) AS pid
		FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id);

                                    $price = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price ASC LIMIT 0,1");
                                    $myprice = mysql_fetch_assoc($price);
                                    $price1 = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price DESC LIMIT 0,1");
                                    $myprice1 = mysql_fetch_assoc($price1);
                                    ?>
                                    <tr>
                                        <td width="3%" align="center" style="padding-left:10px;"><input type="checkbox" name="id[]" value="<?php echo $id; ?>"/></td>
                                        <td width="75" align="center"><img src="<?php echo W_S_IMAGES_DIR . $row_img['thumbnail']; ?>" width="75" height="75" border="0" /></td>
                                        <td align="left" style="padding-left:5px;">
                                            <strong style="color:#06C; min-height: 55px;display: block;"><?php echo stripslashes($product_name) ?></strong>
                                            <div class="actionSingle">
                                                <strong style="color:#F60">
                                                    <?php
                                                    if ($myprice['price'] == $myprice1['price']) {
                                                        echo '$' . $myprice['price'];
                                                    } else {
                                                        echo '$' . $myprice['price'] . ' - $' . $myprice1['price'];
                                                    }
                                                    ?>
                                                </strong> | 
                                                <?php echo stateProduct($id, $pharmacy_id, $status) ?> | 
                                                <?php
                                                if ($owner == $pharmacy_id) {
                                                    $edit = $id;
                                                } else {
                                                    $edit = $id . '&owner=0';
                                                }
                                                ?>

                                                <a href="index.php?view=edit&id=<?php echo $edit; ?>"><i class="icon-pencil"></i> Edit</a> | <a href="<?php echo W_ROOT; ?>/product/product_detail.php?product_id=<?php echo $id ?>" target="_blank">Preview</a> | <a class="delete" onclick="javascript:return confirm('Are you sure do you want to delete product!')" href="index.php?view=delete&id=<?php echo $id ?>"><i class="icon-trash"></i> Delete</a>
                                            </div></td>
                                    </tr>        
                                    <?php
                                    $i++;
                                }// end while
                            } else {
                                ?>
                                <tr>
                                    <td colspan="6" style="height:75px;" align="center"><strong>You don't have any product yet! Please add new product</strong></td>
                                </tr>
                                <?php
                            }
                            ?> 
                        </tbody>       
                    </table>
                </div>
            </div><!--<div class="box">-->
            <div class="row-fluid">
                <div class="span7">
                    <div class="wp-pagenavi">
                        <?php echo $pagingLink; ?>
                        <div class="clear"></div>
                    </div>
                </div>
                <div class="span3 offset2">
                    view per page			
                    <select class="right span4" name="numpage" onchange="javascript:viewByNumPage();">
                        <option value="10" <?php echo($rowsPerPage == 10) ? ' selected="selected"' : ''; ?>>10</option>
                        <option value="20" <?php echo($rowsPerPage == 20) ? ' selected="selected"' : ''; ?>>20</option>
                        <option value="30" <?php echo($rowsPerPage == 30) ? ' selected="selected"' : ''; ?>>30</option>
                        <option value="40" <?php echo($rowsPerPage == 40) ? ' selected="selected"' : ''; ?>>40</option>            
                        <option value="50" <?php echo($rowsPerPage == 50) ? ' selected="selected"' : ''; ?>>50</option>            
                    </select>
                </div>            
            </div>       

        </form>

    </div><!--<div class="box">-->
</div><!--<div class="row-fluid">-->